<?php
$this->breadcrumbs=array(
	'Catálogo'=>array('index'),
	$model->ref,
);
?>

<h1>Ver producto #<?php echo $model->ref; ?></h1>

<div id="tableProduct">
    <?php 
    $category = Category::model()->findByAttributes(array('tag'=>$model->collection));
    $subcategory = Subcategory::model()->findByAttributes(array('tag'=>$model->collection_sub));
    $quoteDollar = Config::model()->findByAttributes(array('tag'=>'dollar_quote'));
    $quoteThaiBaht = Config::model()->findByAttributes(array('tag'=>'thai_baht_quote_to_usd'));
    $priceSet = PriceProduct::model()->findByAttributes(array('id_ref'=>$model->ref));
    $priceWholesale = $priceSet->wholesale_price;
    $priceRetail = $priceSet->retail_price;
    
    if($category == null OR $category->name == 'NO TIENE TRADUCCION'){//TODO ver cuando agregue las cat sin nombre traducido, (encuentra obj pero no tiene nada)
        $nameCategory = $data->collection;
    } else {
        $nameCategory = $category->name;
    }
    if($subcategory == null OR $subcategory->name == 'NO TIENE TRADUCCION'){
        $nameSubcategory = $data->collection_sub;
    } else {
        $nameSubcategory = $subcategory->name;
    }
    
    if(Yii::app()->user->name == 'admin'){
        $valuePrice = round($model->price_thb * $quoteDollar->value * $quoteThaiBaht->value,2);
        $labelPrice = 'Costo (ARS):';
    } else if(Yii::app()->user->name == 'mayorista'){
        if(empty($priceWholesale)){//TODO o si tiene guardado un espacio ' '
            $profitMargin = Config::model()->findByAttributes(array('tag'=>'wholesale_price'));
            $valuePrice = round((($model->price_thb + ($model->price_thb * $profitMargin->value / 100))*$quoteThaiBaht->value)*$quoteDollar->value);
        }else{
            $valuePrice = round($priceWholesale);
        }
        $labelPrice = 'Precio (ARS):';
    } else if(Yii::app()->user->name == 'cliente'){
        if(empty($priceRetail)){//TODO o si tiene guardado un espacio ' '
            $profitMargin = Config::model()->findByAttributes(array('tag'=>'retail_price'));
            $valuePrice = round((($model->price_thb + ($model->price_thb * $profitMargin->value / 100))*$quoteThaiBaht->value)*$quoteDollar->value);
        }else{
            $valuePrice = round($priceRetail);
        }
        $labelPrice = 'Precio (ARS):';
    } else {
        $valuePrice = "???";
        $labelPrice = 'Precio (ARS):';
    }
    if($model->length == 0) {
        $valueLength = 'No asignado';
    } else {
        $valueLength = $model->length.' '.$model->length_unit;
    }
    $this->widget('zii.widgets.CDetailView', array(
            'data'=>$model,
            'attributes'=>array(
                    'ref',
                    array(
                        'name'=>'collection',
                        'label'=>CHtml::encode($model->getAttributeLabel('collection')),
                        'value'=>$nameCategory,
                    ),
                    array(
                        'name'=>'collection_sub',
                        'label'=>CHtml::encode($model->getAttributeLabel('collection_sub')),
                        'value'=>$nameSubcategory,
                    ),
                    array(
                        'name'=>'price_thb',
                        'label'=>$labelPrice,
                        'value'=>$valuePrice,
                    ),
                    'grams',
                    'p_size',
                    array(
                        'name'=>'length',
                        'label'=>'Largo',
                        'value'=>$valueLength,
                    ),
                    'color',
                    'metal',
                    'stone',
                    'comment',
            ),
    )); ?>
    <div id="mainPrice">
        </br>
        <span class="price" style="margin-left: 70%;">$
            <?php if(!Yii::app()->user->isGuest) {
                echo CHtml::encode($valuePrice);
            } else {
                echo CHtml::encode("???");
            }?></span>   
        <!--TODO agregar correctamente el estilo del margen  -->
    </div>
</div>
<div id="imgProduct">
    <img width="447" height="289" class="imgView" src="http://link.ecomsite.net/m1.php?ref=<?php echo CHtml::encode($model->ref); ?>" alt="<?php echo CHtml::encode($model->ref); ?>"></img>
</div>
